package com.lipuwater.agent.ui.activity.install;

import android.graphics.Color;
import com.lipuwater.agent.R;
import com.lipuwater.agent.app.base.adapter.BaseListAdapter;
import com.lipuwater.agent.app.base.ui.BaseListActivity;
import com.lipuwater.agent.app.base.utils.ListSettings;
import com.lipuwater.agent.ble.eventbus.DeviceBindingEvent;
import com.lipuwater.agent.http.bean.install.DeviceInfoBean;
import com.lipuwater.agent.ui.adapter.install.ATInstallDeviceAdapter;

import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * 安装订单设备信息
 */
public class InstallDeviceActivity extends BaseListActivity<DeviceInfoBean> {

    private List<DeviceInfoBean> mData;
    private ATInstallDeviceAdapter adapter;

    @Override
    protected void OnViewCreated() {
        toolbar.setBackgroundResource(R.drawable.img_title_bar);
        initTitle(R.drawable.icon_aleft_white, "设备信息");
        toolbarTitle.setTextColor(Color.parseColor("#ffffff"));
        mData = (List<DeviceInfoBean>) getIntent().getSerializableExtra("deviceList");
        if (mData == null && mData.size() == 0 && mData.get(0) == null) {
            mData = null;
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void requestData(boolean needRefresh) {
        onDataLoaded(mData);
    }

    @Override
    protected BaseListAdapter<DeviceInfoBean> getListAdapter() {
        adapter = new ATInstallDeviceAdapter(this, getIntent().getIntExtra("orderState", 0));
        return adapter;
    }

    @Override
    protected ListSettings getBaseSettings() {
        ListSettings settings = new ListSettings();
        settings.setCanRefresh(false);
        settings.setCanLoadMore(false);
        return settings;
    }

    /**
     * 设备安装通知
     *
     * @param event
     */
    public void onEventMainThread(DeviceBindingEvent event) {
        if (event.getBinding()) {
            if (mData != null && mData.size() > 0 && mData.get(0) != null) {
                int position = adapter.getInstallPosition();
                DeviceInfoBean deviceInfo = mData.get(position);
                deviceInfo.setState("已安装");
                adapter.setOrderState(3);//随便填一个，反正不是1和2就行，1是发货，2是收货
                mData.remove(position);
                mData.add(position, deviceInfo);
                mAdapter.clear();
                onDataLoaded(mData);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
